package com.example.management;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.management.api.ApiRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DeleteActivity extends AppCompatActivity {

    @BindViews({R.id.id_1st, R.id.id_2nd, R.id.id_3rd,
            R.id.id_4th, R.id.id_5th, R.id.id_6th
    })
    List<EditText> mIds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.delete, R.id.clear_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delete:
                List<Integer> ids = new ArrayList<>();
                for (EditText editText : mIds) {
                    String text = editText.getText().toString().trim();
                    if(!text.isEmpty() && text.matches("^[1-9]\\d*$")){
                        ids.add(Integer.valueOf(text));
                    }
                }
                if(ids.size() == 0){
                    return;
                }
                Integer[] arrays = new Integer[ids.size()];
                ids.toArray(arrays);
                ApiRequest.getInstance()
                        .deleteRawMeterial(arrays)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new Subscriber<Object>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                Log.d("RAW", e.getMessage());
                            }

                            @Override
                            public void onNext(Object result) {
                                try {
                                    JSONObject json = new JSONObject(result.toString());
                                    int code = json.optInt("code");
                                    Log.d("RAW", "点击删除， 服务器返回 code ---> " + code);
                                    Toast.makeText(DeleteActivity.this, "服务器返回 --->" + code, Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                break;
            case R.id.clear_text:
                for (EditText editText : mIds) {
                    if (!editText.getText().toString().trim().isEmpty()) {
                        editText.setText("");
                    }
                }
                break;
        }
    }
}
